package com.itouch.bracelet.diseaseinfo;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.activity.BaseActivity;
import com.itouch.bracelet.bean.DiseaseInfoDetail;
import com.itouch.bracelet.result.SearchArrayResult;
import com.itouch.util.DateUtils;
import com.itouch.widget.XListView;
import com.itouch.widget.XListView.IXListViewListener;

public class SearchActivity extends BaseActivity implements OnClickListener,
		IXListViewListener, OnItemClickListener {

	Button btn_back, btn_chat, btn_search;
	EditText edit_search;
	XListView listView;
	RequestQueue mVolleyQueue;
	DiseaseInfoDetailAdapter mAdapter;
	private static String TAG = SearchActivity.class.getName();
	SearchActivity mThis;

	ArrayList<DiseaseInfoDetail> list = new ArrayList<DiseaseInfoDetail>();

	String keyWords = "";
	int page = 0;

	@Override
	protected int setContentViewById() {
		// TODO Auto-generated method stub
		mVolleyQueue = Volley.newRequestQueue(this);
		return R.layout.activity_search;
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		mThis = this;
		btn_back = (Button) findViewById(R.id.back);
		btn_back.setOnClickListener(this);

		btn_search = (Button) findViewById(R.id.btn_search);
		btn_search.setOnClickListener(this);

		edit_search = (EditText) findViewById(R.id.edit_search);

		listView = (XListView) findViewById(R.id.list);
		listView.setPullLoadEnable(false);
		listView.setPullRefreshEnable(true);
		listView.setXListViewListener(this);
		listView.setOnItemClickListener(this);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mAdapter = new DiseaseInfoDetailAdapter(mThis, list);
		listView.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.btn_search:
			keyWords = edit_search.getText().toString();
			search();
			break;

		default:
			break;
		}
	}

	public void search() {
		list.clear();
		getListRequest(keyWords, 1);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		list.clear();
		getListRequest(keyWords, 1);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		getListRequest(keyWords, ++page);
	}

	/**
	 * 获取活动列表
	 * 
	 * @param a
	 */
	private void getListRequest(String keyWords, int page) {
		show();
		List<NameValuePair> lists = new ArrayList<NameValuePair>();
		String url = Constants.URL + Constants.DISEASEINFO_SEARCH;
		Uri.Builder builder = Uri.parse(url).buildUpon();

		builder.appendQueryParameter("filter_S_title", keyWords);
		builder.appendQueryParameter("page", page + "");
		builder.appendQueryParameter("rows", Constants.SIZE + "");

		Log.e(TAG, TAG + builder.toString() + "&filter_S_title=" + keyWords + "&page=" + page + "&rows=" + 10);
		JsonObjectRequest jsonObjRequest = new JsonObjectRequest(
				Request.Method.GET, builder.toString(), null,
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						disMiss();
						stopLoad();
						SearchArrayResult result = new Gson().fromJson(
								response.toString(), SearchArrayResult.class);
						if (result == null) {
							return;
						}
						List<DiseaseInfoDetail> resultList = result.getRows();
						if (null != resultList) {
							int resultSize = resultList.size();
							list.addAll(resultList);
							if (resultSize < Constants.SIZE) {
								listView.setPullLoadEnable(false);
							} else {
								listView.setPullLoadEnable(true);
							}
						}
						mAdapter.refreshData(list);
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						disMiss();

					}
				});
		mVolleyQueue.add(jsonObjRequest);
	}

	private void stopLoad() {
		if (null != listView) {
			listView.stopRefresh();
			listView.stopLoadMore();
			listView.setRefreshTime(DateUtils.getCrrentTimeStr());
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		if(arg2 == 0)
		{
			return;
		}
		Intent intent = new Intent(mThis, DiseaseInfoDetailActivity.class);
		intent.putExtra("data", list.get(arg2-1));
		startActivity(intent);
	}

}
